<?php
namespace hwapp\modules\hwapi\controllers;

use app\models\EcsUsers;
use app\models\lib\AuthLib;
use app\models\TDetectionHeartrate;
use app\models\TDetectionOvulation;
use app\models\TDetectionPregnant;
use app\models\TDetectionUrine;
use yii\base\Exception;
use yii\web\Controller;

/**
 * Class BaseController
 * @package frontend\modules\base\controller
 */
class BaseController extends Controller
{

    public $enableCsrfValidation = false;
    /**
     * 删除用户检测数据
     * @desc 删除用户检测数据接口
     * @param string $timeStamp |时间戳|yes|10位整数|
     * @param string $randomStr |随机数|yes|8位|
     * @param string $signature |签名|yes|32位|
     * @param string $userid |用户授权openId|yes|
     * @return array code 错误码  code=401：验证失败 code=200：通过验证
     * @return string msg 提示信息
     * @exception 400 参数传递错误
     * @exception 500 服务器内部错误
     *
     */
    public function actionDeleteudata()
    {
        header('Access-Control-Allow-Origin:*');

        try{
            $auth = new AuthLib();
            //验证身份
            $timeStamp = isset($_POST['timeStamp'])?$_POST['timeStamp']:'';
            $randomStr = isset($_POST['randomStr'])?$_POST['randomStr']:'';
            $signature = isset($_POST['signature'])?$_POST['signature']:'';
            $userid = isset($_POST['userid'])?$_POST['userid']:'';
            if($userid=="" || $signature=="" || $randomStr=="" || $timeStamp==""){
                return json_encode(['code'=>400,'msg'=>'缺少参数！']);
            }
            $str = $auth -> arithmetic($timeStamp,$randomStr);
            if($str != $signature){
                return json_encode(['code'=>401,'msg'=>'签名错误！']);
            }

            if($_POST) {
                $userid = EcsUsers::find()->where(['open_id' =>$userid,'login_type'=>3])->one()->user_id;
//            $userid = EcsUsers::find()->where(['open_id' =>'MDFAMTAwMjQwOTA5QGE5NTVlNTU3NTliaN2QyZDEyOTMzNjE4MGZhN2EzMmMwQDU2NzA0ZThhNmNiaYmJmMjgzMzAxNDZmZDI5MmRjZDFmNTUzYjc0YjNmMDdhNDU4NmQ0ODM4M2Zh','login_type'=>3])->one()->user_id;
                TDetectionHeartrate::deleteAll(['user_id'=>$userid]);
                TDetectionOvulation::deleteAll(['user_id'=>$userid]);
                TDetectionPregnant::deleteAll(['user_id'=>$userid]);
                TDetectionUrine::deleteAll(['user_id'=>$userid]);
            }
            return json_encode(['code'=>200,'msg'=>'检测数据删除成功！']);
        } catch(Exception $e){

        }

    }
}